package zhu.areas.hatchnav.action;

import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;

import zhu.areas.dic.data.ArticleTopic;
import zhu.areas.dic.data.ArticleType;
import zhu.areas.hatchnav.data.Article;
import zhu.areas.hatchnav.data.ArticleComm;
import zhu.areas.hatchnav.service.IArticleCommService;
import zhu.areas.hatchnav.service.IArticleRankService;
import zhu.areas.hatchnav.service.IArticleShowService;
import zhu.common.data.Pager;
import zhu.common.service.IDicService;
import com.opensymphony.xwork2.ActionSupport;

/*
 * 显示文章详细信息，包括评论信息的显示和发布
 */
/**
 * 
 * @author liujiang
 *
 */
public class ShowArticleDetailAction extends ActionSupport {
	private IArticleCommService articleCommService;
	private IArticleShowService articleShowService;
	private IArticleRankService articleRankService;
	private IDicService dicService;

	private int articleID;// 文章ID
	private String articleTitle;
	private String partialContent;
	private String articleContent;
	private int publisher;// 发布人ID
	private String publisherName;
	private Date pubTime;
	private int artType;
	private String artTypeName;//文章类型的名称
	private int visitNum;
	
	private int articleTopic;//文章主题ID
	private String articleTopicName;

	private int commUID;
	private String commUName;
	private String commContent;

	private List<ArticleComm> artCommList;
	private ArticleComm articleComm;
	private Article article;

	private Pager pager;
	private String currentPage;
	private String pageMethod;
	private Integer pageSize = 4;// 每页显示的记录数目
	
	private int visitNumAdd;//访问量增加量
	
	private List<Article> articleTopList;//top文章列表
	private List<Article> articleRelatedTop4;//访问量排前四的相关文章列表
	private Integer topCount=5;

	// 显示文章详细信息以及所有的评论信息
	public String showArticleDetail() {
		
		//查询排名top5的文章列表
		this.articleTopList = this.articleRankService.findTopArticlesByVisitNum(topCount);
		
		articleID = (Integer) ServletActionContext.getRequest().getSession().getAttribute("articleID");
		//修改访问量
		if(visitNumAdd==1)
		{
			this.articleRankService.incArticleVisitNum(articleID);
		}
		
		article = this.articleShowService.findArticleById(articleID);
		convertArticleToString(article);
		
		System.out.println("articleID: "+articleID);
		System.out.println("pageSize: "+pageSize);
		System.out.println("currentPage: "+currentPage);
		System.out.println("pageMethod: "+pageMethod);
		
		
		pager = this.articleCommService.findPageArticleComm(articleID,
				pageSize, currentPage, pageMethod);
		artCommList = this.pager.getList();
		
		articleRelatedTop4 = this.articleRankService.findTop4ArticlesByTopic(article.getArticleTopic().getArtTopicID());
		
		this.setCurrentPage(String.valueOf(this.pager.getCurrentPage()));
		return SUCCESS;
	}

	// 添加文章评论信息
	public String addArticleComm() {
		articleComm = new ArticleComm();
		this.convertStringToArticleComm(articleComm);
		this.articleCommService.addArticleComm(articleComm);
		return SUCCESS;
	}

	// 将从页面获取地数据封装成ArticleComm对象
	private void convertStringToArticleComm(ArticleComm articleComm) {
		Integer commUID = (Integer) ServletActionContext.getRequest()
				.getSession().getAttribute("userID");
		String commUName = ServletActionContext.getRequest().getSession()
				.getAttribute("userName").toString();
		Integer articleID = (Integer) ServletActionContext.getRequest()
				.getSession().getAttribute("articleID");
		System.out.println("articleID:"+articleID);
		articleComm.setCommContent(this.getCommContent());
		articleComm.setCommUserID(this.getCommUID());
		articleComm.setCommUName(this.getCommUName());
		articleComm.setCommTime(new Date());
		articleComm.setArticle(this.articleShowService
				.findArticleById(articleID));
		
		articleComm.setLastComm(this.articleCommService.findArticleCommById(this.getCommUID()));
	}

	// 将从数据库获取地Article对象转化成action中的属性
	private void convertArticleToString(Article article) {

		this.setArticleID(article.getArticleID());
		this.setArticleTitle(article.getArticleTitle());
		this.setPartialContent(article.getPartialContent());
		this.setArticleContent(article.getArticleContent());
		this.setPublisher(article.getPublisher());
		this.setPublisherName(article.getPublisherName());

		ArticleType articleType=(ArticleType)this.dicService.getValueById("ArticleType", "artTypeID", article.getArtType());
		this.setArtTypeName(articleType.getArtTypeName());
		this.setArtType(article.getArtType());
		
		
		ArticleTopic articleTopic=(ArticleTopic)this.dicService.getValueById("ArticleTopic", "artTopicID", article.getArticleTopic().getArtTopicID());
		this.setArticleTopic(article.getArticleTopic().getArtTopicID());
		this.setArticleTopicName(articleTopic.getArtTopicName());
		
		
		this.setPubTime(article.getPubTime());
		this.setVisitNum(article.getVisitNum());
	}

	public IArticleCommService getArticleCommService() {
		return articleCommService;
	}

	public void setArticleCommService(IArticleCommService articleCommService) {
		this.articleCommService = articleCommService;
	}

	public IArticleShowService getArticleShowService() {
		return articleShowService;
	}

	public void setArticleShowService(IArticleShowService articleShowService) {
		this.articleShowService = articleShowService;
	}

	public IArticleRankService getArticleRankService() {
		return articleRankService;
	}

	public void setArticleRankService(IArticleRankService articleRankService) {
		this.articleRankService = articleRankService;
	}

	public int getArticleID() {
		return articleID;
	}

	public void setArticleID(int articleID) {
		this.articleID = articleID;
		ServletActionContext.getRequest().getSession().setAttribute("articleID", articleID);
	}

	public String getArticleTitle() {
		return articleTitle;
	}

	public void setArticleTitle(String articleTitle) {
		this.articleTitle = articleTitle;
	}

	public String getPartialContent() {
		return partialContent;
	}

	public void setPartialContent(String partialContent) {
		this.partialContent = partialContent;
	}

	public String getArticleContent() {
		return articleContent;
	}

	public void setArticleContent(String articleContent) {
		this.articleContent = articleContent;
	}

	public int getPublisher() {
		return publisher;
	}

	public void setPublisher(int publisher) {
		this.publisher = publisher;
	}

	public String getPublisherName() {
		return publisherName;
	}

	public void setPublisherName(String publisherName) {
		this.publisherName = publisherName;
	}

	public Date getPubTime() {
		return pubTime;
	}

	public void setPubTime(Date pubTime) {
		this.pubTime = pubTime;
	}

	public int getArtType() {
		return artType;
	}

	public void setArtType(int artType) {
		this.artType = artType;
	}

	public int getVisitNum() {
		return visitNum;
	}

	public void setVisitNum(int visitNum) {
		this.visitNum = visitNum;
	}

	public int getCommUID() {
		return commUID;
	}

	public void setCommUID(int commUID) {
		this.commUID = commUID;
	}

	public String getCommUName() {
		return commUName;
	}

	public void setCommUName(String commUName) {
		this.commUName = commUName;
	}

	public String getCommContent() {
		return commContent;
	}

	public void setCommContent(String commContent) {
		this.commContent = commContent;
	}

	public List<ArticleComm> getArtCommList() {
		return artCommList;
	}

	public void setArtCommList(List<ArticleComm> artCommList) {
		this.artCommList = artCommList;
	}

	public ArticleComm getArticleComm() {
		return articleComm;
	}

	public void setArticleComm(ArticleComm articleComm) {
		this.articleComm = articleComm;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getPageMethod() {
		return pageMethod;
	}

	public void setPageMethod(String pageMethod) {
		this.pageMethod = pageMethod;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public String getArtTypeName() {
		return artTypeName;
	}

	public void setArtTypeName(String artTypeName) {
		this.artTypeName = artTypeName;
	}

	public IDicService getDicService() {
		return dicService;
	}

	public void setDicService(IDicService dicService) {
		this.dicService = dicService;
	}

	public int getVisitNumAdd() {
		return visitNumAdd;
	}

	public void setVisitNumAdd(int visitNumAdd) {
		this.visitNumAdd = visitNumAdd;
	}

	public List<Article> getArticleTopList() {
		return articleTopList;
	}

	public void setArticleTopList(List<Article> articleTopList) {
		this.articleTopList = articleTopList;
	}

	public List<Article> getArticleRelatedTop4() {
		return articleRelatedTop4;
	}

	public void setArticleRelatedTop4(List<Article> articleRelatedTop4) {
		this.articleRelatedTop4 = articleRelatedTop4;
	}

	public Integer getTopCount() {
		return topCount;
	}

	public void setTopCount(Integer topCount) {
		this.topCount = topCount;
	}

	public int getArticleTopic() {
		return articleTopic;
	}

	public void setArticleTopic(int articleTopic) {
		this.articleTopic = articleTopic;
	}

	public String getArticleTopicName() {
		return articleTopicName;
	}

	public void setArticleTopicName(String articleTopicName) {
		this.articleTopicName = articleTopicName;
	}

}
